package library.controller;

import library.ErrorCode;
import library.ErrorMessage;
import library.entity.Reader;

import java.sql.SQLException;

public class ReaderController extends AbstractController<Reader> {

	public ReaderController() throws SQLException{
		super(Reader.class);
	}

	public Reader getReader(String name){
        try {
            return selectOnce("userName", name);
        } catch (Exception e) {
            return null;
        }
    }

    public boolean create(Reader newReader) {
        if(getReader(newReader.getUserName()) != null){
            lastError = new ErrorMessage(ErrorCode.DUPLICATE_USER_FOUND);
            return false;
        }
        return super.insert(newReader);
    }

    public boolean delete(String deleteReaderName) {
        Reader deleteReader = getReader(deleteReaderName);
        if(deleteReader == null){
            lastError = new ErrorMessage(ErrorCode.READER_NOT_FOUND);
            return false;
        }
        return super.delete(deleteReader);
    }
    
    public boolean update(Reader reader){
		return super.update(reader);
    }
}
